package com.example.blog.controller;

import cn.hutool.core.util.StrUtil;
import com.example.blog.entity.User;
import com.example.blog.entity.UserDTO;
import com.example.blog.service.UserService;
import com.example.blog.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lu
 * @Date: 2023-09-05-9:27
 * @Description:
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户列表
     * @return
     */
    @ApiOperation(value = "用户列表")
    @PostMapping("/list")
    public Result<Object> list(){
        List<User> userList = userService.findAll();
        return Result.success(userList);
     }

    /**
     * 用户列表
     * @return
     */
    @ApiOperation(value = "用户列表")
    @PostMapping("/list1")
    public Result<Object> list1(){
        UserDTO userDTO = userService.find();
        return  Result.success(userDTO);
    }


    /**
     * 添加用户
     * @return
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("/create")
    public Result<Object> userCreate(@RequestBody @Valid User user) {
        if(StrUtil.isEmpty(user.getPassWord())){
            return Result.error("密码不能为空");
        }
        //密码加密存储
        user.setPassWord(MD5Util.MD5(user.getPassWord()));
        //判断手机号
        if(!PhoneUtil.checkMobile(String.valueOf(user.getPhone()))){
            return Result.error("手机号格式不正确");
        }
        userService.createUser(user);
        return Result.success();
    }

    /**
     *
     * 修改用户
     * @return
     */
    @ApiOperation(value = "修改用户")
    @PostMapping("/update")
    public Result<Object> userUpdate(@RequestBody @Valid User user) {
        if (StrUtil.isEmpty(user.getPassWord())) {
            return Result.error("密码不能为空");
        }
        // 密码加密存储
        user.setPassWord(MD5Util.MD5(user.getPassWord()));
        //判断手机号
        if (!PhoneUtil.checkMobile(String.valueOf(user.getPhone()))) {
            return Result.error("手机号格式不正确");
        }
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 删除
     * @return
     */
    @ApiOperation(value = "删除用户")
    @PostMapping("/delete/{id}")
    public Result<Object> userDelete(@PathVariable(value = "id") int id) {
        userService.deleteUser(id);
        return Result.success();
    }

    /**
     * 登录
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public Result<Object> userLogin(@RequestBody @Valid User user){
        List<User> login = userService.login(user);
        if(user.getPassWord().equals("") || user.getUserName().equals("")){
            return  Result.error("用户名或密码不能为空");
        }else{
            if(login.size() != 0){
                return Result.success(login);
            }else{
                return Result.error("用户不存在或密码错误");
            }
        }
        //return JsonResult.success(login);
    }

    @ApiOperation(value = "登录生成token")
    @GetMapping("/login")
    public Map<String, Object> login(User user) {

        Map<String, Object> map = new HashMap<>();
        try {
            User login =  userService.login1(user);
            //User login1 = (User) userService.login(user);

            // 生成 token
            Map<String, String> payload = new HashMap<>();
            payload.put("userName", login.getUserName());
            payload.put("passWord", login.getPassWord());
            String token = JwtUtil.getToken(payload);

            map.put("status", true);
            map.put("msg", "认证成功");
            map.put("token", token);
        } catch (Exception e) {
            map.put("status", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @ApiOperation(value = "测试")
    @PostMapping("/test")
    public Object test(){
        return "访问成功！";
    }



}
